using System;
using INdT.Sparta.Graphics;
using Microsoft.Xna.Framework;
using INdT.Sparta.Input;
using Microsoft.Xna.Framework.Input.Touch;
using System.Runtime.Serialization;

namespace INdT.Sparta.UI
{
    [DataContract]
    public class SpartaButton : SpartaSprite
    {
        public Action<SpartaButton> OnClicked;

        [DataMember]
        public int DefaultFrame { get; set; }
        [DataMember]
        public int DisabledFrame { get; set; }
        [DataMember]
        public int PressedFrame { get; set; }

        private SpartaButtonState state;
        [DataMember]
        public SpartaButtonState State
        {
            get { return state; }
            set
            {
                state = value;

                if (state == SpartaButtonState.Normal)
                {
                    SourceRect = new Rectangle(DefaultFrame * SourceRect.Width, 0, SourceRect.Width, Height);
                }
                else if (state == SpartaButtonState.Disabled)
                {
                    SourceRect = new Rectangle(DisabledFrame * SourceRect.Width, 0, SourceRect.Width, Height);
                }
                else
                {
                    SourceRect = new Rectangle(PressedFrame * SourceRect.Width, 0, SourceRect.Width, Height);
                }
            }
        }

        [DataMember]
        public bool IsToggle { get; set; }

        public SpartaButton()
        {
        }

        public SpartaButton(string imageName)
            : base(0, 0, imageName)
        {
        }

        public SpartaButton(string imageName, Action<SpartaButton> onClicked)
            : base(0, 0, imageName)
        {
            this.OnClicked = onClicked;
        }

        public SpartaButton(string imageName, int width, int height, Action<SpartaButton> onClicked)
            :base(width, height, imageName)
        {
            this.OnClicked = onClicked;
            
            DefaultFrame = 0;
            DisabledFrame = 0;
            PressedFrame = 0;

            SourceRect = new Rectangle(0, 0, width, height);
            
            State = SpartaButtonState.Normal;
            Stop();
        }

        public SpartaButton(string imageName, int width, int height, int defaultFrame, int pressedFrame, Action<SpartaButton> onClicked)
            : base(width, height, imageName)
        {
            this.OnClicked = onClicked;

            this.DefaultFrame = defaultFrame;
            this.DisabledFrame = 0;
            this.PressedFrame = pressedFrame;

            SourceRect = new Rectangle(0, 0, width, height);            

            State = SpartaButtonState.Normal;
            Stop();
        }

        public SpartaButton(string imageName, int width, int height, int defaultFrame, int pressedFrame, int disabledFrame, Action<SpartaButton> onClicked)
            : base(width, height, imageName)
        {
            this.OnClicked = onClicked;

            this.DefaultFrame = defaultFrame;
            this.DisabledFrame = disabledFrame;
            this.PressedFrame = pressedFrame;

            SourceRect = new Rectangle(0, 0, width, height);

            State = SpartaButtonState.Normal;
            Stop();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            TouchCollection touches = SpartaTouch.TouchCollection;
            if (touches.Count > 0)
            {
                if (touches[0].State == TouchLocationState.Pressed)
                {
                    Vector2 point = touches[0].Position;
                    if (BoundingBox.Contains((int)Math.Ceiling(point.X), (int)Math.Ceiling(point.Y)))
                    {
                        if (OnClicked != null)
                        {
                            OnClicked(this);
                        }

                        if (IsToggle)
                        {
                            if (State == SpartaButtonState.Pressed)
                            {
                                State = SpartaButtonState.Normal;
                            }
                            else
                            {
                                State = SpartaButtonState.Pressed;
                            }
                        }
                    }
                }
            }
        }
    }
}
